package hai.wom.org.qyer_home;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


import hai.wom.org.qyer_home.adapter.MyAdapter_Fragment;
import hai.wom.org.qyer_home.adapter.MyAdapter_Img;
import hai.wom.org.qyer_home.adapter.MyAdapter_MainActivity;
import hai.wom.org.qyer_home.bean.GuessLike;
import hai.wom.org.qyer_home.bean.Slide_data;
import hai.wom.org.qyer_home.callback.ICallback;
import hai.wom.org.qyer_home.fragment.Fragment_FindCity;
import hai.wom.org.qyer_home.fragment.Fragment_Free;
import hai.wom.org.qyer_home.fragment.Fragment_Spot;
import hai.wom.org.qyer_home.utils.HttpUtils;

public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener {
    private ListView lv;
    private MyAdapter_Img adapter_img;
    private List<ImageView> list_img;
    private ViewPager vp1_head;

    private ViewPager vp2_head;
    List<Fragment> list_fragment;
    MyAdapter_Fragment adapter_fragment;


    private String path = "http://open.qyer.com/lastminute/home/major?client_id=qyer_discount_androi&client_secret=227097da1d07a2a9860f&track_user_id=&track_deviceid=352284042226724&track_app_version=2.0.4&track_app_channel=qq&track_device_info=hlte&track_os=Android4.4.2&app_installtime=1483531797801&size=720x1242&ra_referer=app_home&lat=39.084881&lon=117.199403&oauth_token=";

    //小圆点
    ImageView iv1_disable;
    ImageView iv2_disable;
    ImageView iv3_disable;
    ImageView iv4_disable;
    //小圆点 ViewPager2
    ImageView iv1_vp2_disable;
    ImageView iv2_vp2_disable;
//景点门票

    FragmentManager manager;


    Handler handler;
    Timer timer;
    int index;


    private String guesslikePath = "http://open.qyer.com/lastminute/app_selected_product?client_id=qyer_discount_androi&client_secret=227097da1d07a2a9860f&track_user_id=&track_deviceid=352284042226724&track_app_version=2.0.4&track_app_channel=qq&track_device_info=hlte&track_os=Android4.4.2&app_installtime=1483531797801&size=720x1242&ra_referer=app_home&lat=39.084881&lon=117.199403&pageSize=10&page=";
    MyAdapter_MainActivity adapter_mainActivity;
    List<GuessLike.DataEntity> list_guesslike;
    int pageCount = 1;
    boolean isFlat = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        lv = (ListView) findViewById(R.id.lv_home);
        View headview = LayoutInflater.from(this).inflate(R.layout.headview_layout, null);
        vp1_head = (ViewPager) headview.findViewById(R.id.vp1_headview);

        iv1_disable = (ImageView) headview.findViewById(R.id.iv1_disable);
        iv2_disable = (ImageView) headview.findViewById(R.id.iv2_disable);
        iv3_disable = (ImageView) headview.findViewById(R.id.iv3_disable);
        iv4_disable = (ImageView) headview.findViewById(R.id.iv4_disable);
        iv1_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);

        list_img = new ArrayList<>();
        adapter_img = new MyAdapter_Img(list_img, this);
        vp1_head.setAdapter(adapter_img);
//轮播
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (index < list_img.size()) {
                    index++;
                } else {
                    index = 0;
                }
                vp1_head.setCurrentItem(index, false);
                return true;
            }
        });
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(110);
            }
        }, 2000, 2000);


        vp1_head.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        iv1_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);
                        iv2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv3_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv4_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        break;
                    case 1:
                        iv1_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);
                        iv3_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv4_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        break;
                    case 2:
                        iv1_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv3_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);
                        iv4_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        break;
                    case 3:
                        iv1_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv3_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv4_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);
                        break;
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        new HttpUtils(new ICallback() {
            @Override
            public void getData(String result) {

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject data = jsonObject.getJSONObject("data");
                    JSONObject head_slide = data.getJSONObject("head_slide");
                    JSONArray slide_data = head_slide.getJSONArray("slide_data");
                    for (int i = 0; i < slide_data.length(); i++) {
                        JSONObject js = slide_data.getJSONObject(i);
                        Slide_data.DataEntity.HeadSlideEntity.SlideDataEntity sd = new Slide_data.DataEntity.HeadSlideEntity.SlideDataEntity();
                        sd.setCover(js.getString("cover"));
                        ImageView iv = new ImageView(MainActivity.this);
                        Picasso.with(MainActivity.this).load(sd.getCover()).into(iv);
                        list_img.add(iv);
                    }
                    adapter_img.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).execute(path);

//第二个ViewPager
        vp2_head = (ViewPager) headview.findViewById(R.id.vp2_headview);
        iv1_vp2_disable = (ImageView) headview.findViewById(R.id.iv1_vp2_disable);
        iv2_vp2_disable = (ImageView) headview.findViewById(R.id.iv2_vp2_disable);
        iv1_vp2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);

        list_fragment = new ArrayList<>();
        list_fragment.add(new Fragment_Free());
        list_fragment.add(new Fragment_FindCity());
        adapter_fragment = new MyAdapter_Fragment(getSupportFragmentManager(), list_fragment);
        vp2_head.setAdapter(adapter_fragment);
        vp2_head.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        iv1_vp2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);
                        iv2_vp2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        break;
                    case 1:
                        iv1_vp2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_disable);
                        iv2_vp2_disable.setImageResource(R.mipmap.switchbtn_md_switch_thumb_off);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //景点门票
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_spots, new Fragment_Spot());
        transaction.commit();


        lv.addHeaderView(headview);
        list_guesslike = new ArrayList<>();
        adapter_mainActivity = new MyAdapter_MainActivity(list_guesslike, this);
        lv.setAdapter(adapter_mainActivity);
        loadGuessLike();
        lv.setOnScrollListener(this);


    }

    //猜你喜欢！
    private void loadGuessLike() {
        new HttpUtils(new ICallback() {
            @Override
            public void getData(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject js = data.getJSONObject(i);
                        GuessLike.DataEntity dataEntity = new GuessLike.DataEntity();
                        dataEntity.setPic(js.getString("pic"));
                        dataEntity.setTitle(js.getString("title"));
                        dataEntity.setPrice(js.getString("price"));
                        dataEntity.setSale_count(js.getString("sale_count"));
                        list_guesslike.add(dataEntity);

                    }
                    adapter_mainActivity.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).execute(guesslikePath + pageCount);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (isFlat && scrollState == 0) {
            pageCount++;
            loadGuessLike();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        isFlat = (firstVisibleItem + visibleItemCount) == totalItemCount;
    }
}